//
//  hamsBaseballAppDelegate.m
//  hamsBaseball
//
//  Created by Donggi Hahm on 10. 04. 27.
//  Copyright ComTalk 2010. All rights reserved.
//

#import "hamsBaseballAppDelegate.h"
#import "cocos2d.h"
#import "SceneMenu.h"
//#import "FlurryAPI.h"
#import "SimpleAudioEngine.h"
#import "OpenFeint.h"
#import "SampleOFDelegate.h"
#import "global.h"

@implementation hamsBaseballAppDelegate

@synthesize window;

//void uncaughtExceptionHandler(NSException *exception) {
//    [FlurryAPI logError:@"Uncaught" message:@"Crash!" exception:exception];
//}

- (void) applicationDidFinishLaunching:(UIApplication*)application
{
//	// Flurry Analytics
//	NSSetUncaughtExceptionHandler(&uncaughtExceptionHandler);
//	[FlurryAPI startSession:@"EPU9B96C735Y1SMKKBB2"];
	
	// Init the window
	window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
	
	// cocos2d will inherit these values
	[window setUserInteractionEnabled:YES];	
	[window setMultipleTouchEnabled:YES];
	
	// Try to use CADisplayLink director
	// if it fails (SDK < 3.1) use the default director
	if( ! [CCDirector setDirectorType:CCDirectorTypeDisplayLink] )
		[CCDirector setDirectorType:CCDirectorTypeDefault];
	
	// Use RGBA_8888 buffers
	// Default is: RGB_565 buffers
	[[CCDirector sharedDirector] setPixelFormat:kPixelFormatRGBA8888];
	
	// Create a depth buffer of 16 bits
	// Enable it if you are going to use 3D transitions or 3d objects
//	[[CCDirector sharedDirector] setDepthBufferFormat:kDepthBuffer16];
	
	// Default texture format for PNG/BMP/TIFF/JPEG/GIF images
	// It can be RGBA8888, RGBA4444, RGB5_A1, RGB565
	// You can change anytime.
	[CCTexture2D setDefaultAlphaPixelFormat:kTexture2DPixelFormat_RGBA8888];
	
	// before creating any layer, set the landscape mode
	[[CCDirector sharedDirector] setDeviceOrientation:CCDeviceOrientationPortrait];
	[[CCDirector sharedDirector] setAnimationInterval:1.0/60];
	[[CCDirector sharedDirector] setDisplayFPS:NO];
	
	// create an openGL view inside a window
	[[CCDirector sharedDirector] attachInView:window];	
	[window makeKeyAndVisible];	
		
	//kinds of pitch
	gameInfo.blnFastball = TRUE;
	gameInfo.blnCurveball = TRUE;
	gameInfo.blnSlider = TRUE;
	gameInfo.blnChangeup = TRUE;
	
	//openfeint
	NSDictionary* settings = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:UIDeviceOrientationPortrait], OpenFeintSettingDashboardOrientation, [NSNumber numberWithBool:YES],
							  OpenFeintSettingDisableUserGeneratedContent, nil];
	ofDelegate = [SampleOFDelegate new];
	OFDelegatesContainer* delegates = [OFDelegatesContainer containerWithOpenFeintDelegate:ofDelegate];
	[OpenFeint initializeWithProductKey:@"vUp05JKtqc40135Sr2K7mQ"
							  andSecret:@"PgLDa6pGZ2mO0AdC7b7xVJFLmQ1JInSK5cy6wVH2A"
						 andDisplayName:@"iBattingCage"
							andSettings:settings    // see OpenFeintSettings.h
						   andDelegates:delegates]; // see OFDelegatesContainer.h
		
	[[CCDirector sharedDirector] runWithScene: [Menu scene]];
}


- (void)applicationWillResignActive:(UIApplication *)application 
{
	[[SimpleAudioEngine sharedEngine] stopBackgroundMusic];
	[[CCDirector sharedDirector] pause];
//	[OpenFeint applicationWillResignActive]; 
}

- (void)applicationDidBecomeActive:(UIApplication *)application 
{
	[[CCDirector sharedDirector] resume];
//	[OpenFeint applicationDidBecomeActive];
}

- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {
	[[CCTextureCache sharedTextureCache] removeUnusedTextures];
}

- (void)applicationWillTerminate:(UIApplication *)application 
{
	[[SimpleAudioEngine sharedEngine] stopBackgroundMusic];
	[[CCDirector sharedDirector] end];
	 [[NSUserDefaults standardUserDefaults] synchronize];
}

- (void)applicationSignificantTimeChange:(UIApplication *)application {
	[[CCDirector sharedDirector] setNextDeltaTimeZero:YES];
}

- (void)dealloc {
	[[CCDirector sharedDirector] release];
	[window release];
	[super dealloc];
}

@end
